package com.aaa.rsatest.mdm;

import android.content.Context;

import com.aaa.lib_java.android.Base64;
import com.aaa.lib_java.mdm.Encrypt;
import com.aaa.rsatest.SPUtils;

import java.util.Arrays;

public class About {
    public void test(Context context) {
        SPUtils spUtils = SPUtils.getInstance(context);
        try {
            System.out.println("-------------公钥加密，base64传输，私钥解密--------------------------------");
//            String base64Encode = Encrypt.getBase64Encode("010287");
//            System.out.println("getBase64Encode=" + base64Encode);

            String pwd = "010287";
            System.out.println("加密前=" + pwd);

            byte[] encrpy222 = Encrypt.encrypt(Encrypt.loadPublicKey(spUtils.getPublicKey()), pwd.getBytes());
            pwd = new String(encrpy222);
            System.out.println("加密=" + pwd);
            String string = Base64.encodeToString(encrpy222, Base64.DEFAULT);
            System.out.println("加密并base64=" + string);

            byte[] decode = Base64.decode(string.getBytes(), Base64.DEFAULT);
            byte[] decrypt = Encrypt.decrypt(Encrypt.loadPrivateKey(spUtils.getPrivateKey()), decode);
            pwd = new String(decrypt);
            System.out.println("解密=" + pwd);

            System.out.println("-------------公钥加密，hex传输，私钥解密--------------------------------");

            pwd = "010287";
            System.out.println("加密前=" + pwd);

            encrpy222 = Encrypt.encrypt(Encrypt.loadPublicKey(spUtils.getPublicKey()), pwd.getBytes());
            pwd = new String(encrpy222);
            System.out.println("加密=" + pwd);
//                    string = Base64.encodeToString(encrpy222, Base64.DEFAULT);
            string = Encrypt.byte2hex(encrpy222);
            System.out.println("加密并hex=" + string);
//                    decode = Base64.decode(string.getBytes(), Base64.DEFAULT);
            decode = Encrypt.hex2byte(string);

            decrypt = Encrypt.decrypt(Encrypt.loadPrivateKey(spUtils.getPrivateKey()), decode);
            pwd = new String(decrypt);
            System.out.println("解密=" + pwd);

            System.out.println("------------私钥加密，base64传输，公钥解密---------------------------------------------");

            pwd = "010287";
            System.out.println("加密前=" + pwd);
            byte[] encrpy = Encrypt.encrpy(Encrypt.loadPrivateKey(spUtils.getPrivateKey()), pwd.getBytes());
            System.out.println("只加密=" + Arrays.toString(encrpy));
            string = Base64.encodeToString(encrpy, Base64.DEFAULT);
            System.out.println("加密并base64=" + string);

            decode = Base64.decode(string.getBytes(), Base64.DEFAULT);
            decrypt = Encrypt.decrypt(Encrypt.loadPublicKey(spUtils.getPublicKey()), decode);
            pwd = new String(decrypt);
            System.out.println("解密=" + pwd);

            System.out.println("-------------------私钥加密，byte[]转String传输，公钥解密--------------------------------------");

            pwd = "010287";
            System.out.println("加密前=" + pwd);
            encrpy = Encrypt.encrpy(Encrypt.loadPrivateKey(spUtils.getPrivateKey()), pwd.getBytes());
//                    string = Base64.encodeToString(encrpy, Base64.DEFAULT);
//                    string = new String(encrpy);
            string = Encrypt.byte2hex(encrpy);
            System.out.println("密文：加密并byte2hex=" + string + "----");

            System.out.println("公钥=" + spUtils.getPublicKey());
            System.out.println("私钥=" + spUtils.getPrivateKey());

//                    decode = Base64.decode(string.getBytes(), Base64.DEFAULT);
//                    decode = string.getBytes();
            decode = Encrypt.hex2byte(string);
            decrypt = Encrypt.decrypt(Encrypt.loadPublicKey(spUtils.getPublicKey()), decode);
            pwd = new String(decrypt);
            System.out.println("解密=" + pwd);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
